package com.greyu.ysj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtAsset;
import com.greyu.ysj.entity.CbtUserStar;
import com.greyu.ysj.entity.CbtUserStarExample;

import com.greyu.ysj.mapper.CbtUserStarMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtUserStarService;
@Service
public class CbtUserStarServiceImpl implements CbtUserStarService {
	@Autowired
	CbtUserStarMapper cbtUserStarMapper;

	@Override
	public CbtUserStar selectAllByUserId(Integer userId) {
		 CbtUserStarExample cbtUserStarExample = new CbtUserStarExample();
		 CbtUserStarExample.Criteria criteria = cbtUserStarExample.createCriteria();
	        criteria.andUserIdEqualTo(userId);
	        List<CbtUserStar> cbtUserStars = this.cbtUserStarMapper.selectByExample(cbtUserStarExample);

	        CbtUserStar cbtUserStar;
	        try {
	        	cbtUserStar = cbtUserStars.get(0);
	        } catch (Exception e) {
	            return null;
	        }
	        return cbtUserStar;
	}
	
	public ResultModel save(CbtUserStar cbtUserStar) {
		
		//insert
        this.cbtUserStarMapper.insert(cbtUserStar);
        return ResultModel.ok(cbtUserStar);
}

	@Override
	public ResultModel update(Integer userId,CbtUserStar cbtUserStar) {
		// TODO Auto-generated method stub
		 CbtUserStarExample cbtUserStarExample = new CbtUserStarExample();
		 CbtUserStarExample.Criteria criteria = cbtUserStarExample.createCriteria();
	        criteria.andUserIdEqualTo(userId);
	        List<CbtUserStar> cbtUserStars = this.cbtUserStarMapper.selectByExample(cbtUserStarExample);
	        if(cbtUserStars.size()>0){
	        	   CbtUserStar cbtUserStar2 = cbtUserStars.get(0);
	       		if(cbtUserStar.getAccount()!= null) {
	       			cbtUserStar2.setAccount(cbtUserStar.getAccount());	
	       		}
	    		this.cbtUserStarMapper.updateByPrimaryKey(cbtUserStar2);
	    		 return ResultModel.ok(cbtUserStar2);
	        }
	     
	        return ResultModel.ok(null);
		
	}

}
